from epics import PV 
from logger import getLogger
import requests
import time
logger = getLogger("PV")

url = "http://127.0.0.1:8099/api/modeconf/notice"
class PV_Class():
    def __init__(self):

        self._uuid = "TEST:Mode:uuid"
        self.task_id_conn = False
        self.uuid_conn = False
        self.uuid = PV(self._uuid,callback=self.onValueChange,connection_callback=self.onConnectionChange)
        logger.info("Init finish")
    def onConnectionChange(self,pvname=None ,conn= None,**kws):
        if pvname == self._uuid:
            self.uuid_conn = conn
    def onValueChange(self,pvname=None, value=None, host=None, **kws):
        if self.uuid_conn:
            if not len(self.uuid.get(as_string = True)) == 0:
                params = {"task_id":self.uuid.get(as_string = True).replace(" ","").split("&")[0],"uuid":self.uuid.get(as_string = True).replace(" ","").split("&")[1],"source":1}
                res = requests.get(url = url,params = params)
                logger.info("data:"+str(params)+"\tresult:"+str(res.text))
        
if __name__ == "__main__":
    pv = PV_Class()
    while True:
        time.sleep(1)
